<?php
declare (strict_types = 1);

namespace app\controller;

use app\BaseController;
use app\modelservice\LoginService;
use app\validate\LoginValidate;

class Login extends BaseController
{
    public $loginService;

    public function initialize()
    {
        $this->loginService = new LoginService;
    }
    
    public function login()
    {
        $params = request()->only(['username', 'password']);

        try {
            checkRequest('post');
            validate(LoginValidate::class)->scene('login')->check($params);

            $token = $this->loginService->login($params);
            return success(['token' => $token]);
            
        } catch (\Throwable $th) {
            return error($th->getMessage(), BOOL_FALSE);
        }
    }

    public function logout()
    {
        return success(null, 'ok');
    }
}
